#pragma once

using namespace System;
using namespace System::Runtime::InteropServices;

#define VPX_CODEC_DISABLE_COMPAT 1
#include<vpx\vpx_encoder.h>
#include<vpx\vpx_decoder.h>
#include<vpx\vp8cx.h>
#include<vpx\vp8dx.h>

namespace NVP8 
{
	public ref class Packet
	{
	public:
		void Init(const vpx_codec_cx_pkt* packetData)
		{
			this->packetData = packetData;
		}

		int WriteAsIVF(array<Byte>^ data, int offset, int length)
		{
			if(packetData->kind != VPX_CODEC_CX_FRAME_PKT)
				return 0;
 
			// write IVF header
			Array::Copy(BitConverter::GetBytes(packetData->data.frame.sz), 0, data, offset, sizeof(size_t));
			offset += sizeof(size_t);
			vpx_codec_pts_t pts = packetData->data.frame.pts;
			Array::Copy(BitConverter::GetBytes(pts), 0, data, offset, sizeof(vpx_codec_pts_t));
			offset += sizeof(vpx_codec_pts_t);
 
			// write payload
			Marshal::Copy((IntPtr)packetData->data.frame.buf, data, offset, packetData->data.frame.sz);
			return sizeof(size_t) + sizeof(vpx_codec_pts_t) + packetData->data.frame.sz;
		}

		int WriteRaw(array<Byte>^ data, int offset, int length)
		{
			if(packetData->kind != VPX_CODEC_CX_FRAME_PKT)
				return 0;

			Marshal::Copy((IntPtr)packetData->data.frame.buf, data, offset, packetData->data.frame.sz);
			return packetData->data.frame.sz;
		}

	private:
		const vpx_codec_cx_pkt* packetData;
	};
}